//
//  CYMainViewController.swift
//  MySina
//
//  Created by mac on 2017/1/16.
//  Copyright © 2017年 xinyuan. All rights reserved.
//

import UIKit

class CYMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        //设置tabBar
        let tabBar = CYTabBar()
        setValue(tabBar, forKey: "tabBar")
        //02.实力化闭包
        tabBar.closure = {
            print("闭包小朋友,我是Main控制器,我知道是➕被点击了")
        }
        //代理05 - 设置代理
        tabBar.CYDelegate = self
        
        addChildViewController(CYHomeViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(CYMessageViewController(), imageName: "tabbar_message_center", title: "信息")
        addChildViewController(CYDiscoverViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(CYMineViewController(), imageName: "tabbar_profile", title: "我的")
        
    }


}

extension CYMainViewController {
    
    // MARK: - 添加子控制器
    func addChildViewController(_ childController: UIViewController, imageName: String, title: String) {
        //设置子控制器tabbar文字和颜色
        childController.title = title
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: OrangeColor], for: UIControlState.selected)
        
        //设置子控制器tabbar图片和样式
        childController.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        
        //添加子控制器
        addChildViewController(CYMainNavigationController(rootViewController: childController))
        
    }
    
}

//代理06 - 遵守代理协议,实现代理方法
extension CYMainViewController: CYTabBarDelegate {
    
    func composeButtonDidClick() {
        print("代理小朋友,我是Main控制器,我知道是➕被点击了")
    }
}







